package com.sensor.work.consumer;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sensor.work.dto.DeviceDataDto;
import com.sensor.work.entity.PudongDeviceConfig;
import com.sensor.work.entity.PudongDeviceData;
import com.sensor.work.service.IPudongDeviceConfigService;
import com.sensor.work.service.IPudongDeviceDataService;
import com.sensor.work.service.QcServer;
import lombok.AllArgsConstructor;

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.builder.BaseBuilder;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.utils.JavaUtils;
import org.springframework.amqp.utils.SerializationUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@RabbitListener(queues  ={"deviceQueue"})
@AllArgsConstructor
@Slf4j
public class DeviceDataConsumer {

    @Autowired
    private IPudongDeviceDataService pudongDeviceDataService;

    @Autowired
    private IPudongDeviceConfigService deviceService;

    @Autowired
    private IPudongDeviceConfigService pudongDeviceConfigService;

    @Autowired
    private QcServer qcServer;


    @RabbitHandler
    public void process(Map<String,Object> message) {
//        try {



            JSONObject jsonObject = JSONUtil.parseObj(message);
            Object deviceId = message.get("deviceId");
            log.info("接收端SN{}",deviceId);
            log.info("接收端data数据{}",message);
        List<PudongDeviceConfig> deviceNo = pudongDeviceConfigService.list(new QueryWrapper<PudongDeviceConfig>().eq("device_no", deviceId));
        int size = deviceNo.size();

            if (size==0){
                log.warn(deviceId+"查无此设备");
                return;
            }

            JSONObject payload = (JSONObject)jsonObject.get("payload");
            payload.set("deviceNo",deviceId);
        PudongDeviceConfig deviceInfo = deviceNo.get(0);
//
            DeviceDataDto bean = payload.toBean(DeviceDataDto.class);
              PudongDeviceData pudongDeviceData =new PudongDeviceData();
              BeanUtils.copyProperties(bean,pudongDeviceData);
//            log.info("转换实体类数据{}",pudongDeviceData);


            boolean b = pudongDeviceDataService.save(pudongDeviceData);
              log.info("进行质控前{}",pudongDeviceData);
                //进行质控
            if (b){
                log.info(deviceId+"进行质控");

                boolean save = qcServer.deviceQcControl(pudongDeviceData, deviceInfo);


            }else {
                log.warn(deviceId+"新增失败");
            }
//            UpdateWrapper<PudongDeviceConfig> objectUpdateWrapper = new UpdateWrapper<>();
//            objectUpdateWrapper.set("device_ctwing_id",deviceId);
//            objectUpdateWrapper.set("status",1);
//            deviceService.update(objectUpdateWrapper);
//        }catch (Exception e) {
//            log.error("错误信息{}",e.getMessage());
//        }
    }

}
